<template>
  <el-dialog title="治疗信息" :visible.sync="visible" width="60%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="110px">
      <el-form-item label="宠物主人" prop="nickName">
        <el-input v-model="dataForm.nickName" disabled placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="宠物名称" prop="petName">
        <el-input v-model="dataForm.petName" disabled placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="宠物身高" prop="height">
        <el-input v-model="dataForm.height" clearable placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="宠物体重" prop="weight">
        <el-input v-model="dataForm.weight" clearable placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="病因" prop="petIntro">
        <el-input v-model="dataForm.petIntro" type="textarea" :rows="4" clearable placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="治疗措施" prop="measure">
        <el-input v-model="dataForm.measure" type="textarea" :rows="4" clearable placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="用药" prop="prescription">
        <el-select v-model="dataForm.prescription" multiple placeholder="请选择">
          <el-option
            v-for="(item,index) in goodsList"
            :key="item.id"
            :label="item.name"
            :value="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="治疗时间" prop="endTime">
        <el-date-picker
          v-model="dataForm.endTime"
          type="date"
          disabled
          placeholder="选择日期"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer" v-if="!dataForm.status">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDataInfo, savaDataInfo } from "@/api/apply";
import { mapGetters } from "vuex";
import { getDataList as getGoodsList } from "@/api/hospital/goods";
import { savaDataInfo as updateGoods } from "@/api/hospital/goods";
import { watch } from "vue";
export default {
  data() {
    return {
      visible: false,
      dataForm: {},
      goodsList: [],
      dataRule: {}
    };
  },
  computed: {
    ...mapGetters(["userInfo"])
  },
  methods: {
    // 获取药品数据
    getGoodsData() {
      getGoodsList({
        limit: 1000,
        page: 1
      })
        .then(res => {
          if (res && res.code === 0) {
            this.goodsList = (res.page && res.page.list) || [];
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    getTime() {
      let date = new Date();
      let year = date.getFullYear(),
        month = date.getMonth(),
        day = date.getDay(),
        h = date.getHours(),
        m = date.getMinutes(),
        s = date.getSeconds();
      return `${year}-${month}-${day} ${h}:${m}:${s}`;
    },
    init(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.dataForm = { id, endTime: this.getTime() };
        this.getGoodsData();
        if (id) {
          getDataInfo(id)
            .then(data => {
              if (data && data.code === 0) {
                this.dataForm = data.data;
                if (!data.data.status) {
                  this.dataForm.endTime = this.getTime();
                }
              }
            })
            .catch(err => {
              console.log(err);
            });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.dataForm.status = 1;
          let temp =
            (this.dataForm.prescription && this.dataForm.prescription) || [];
          let tempPerArr = [];
          temp.forEach(i => {
            tempPerArr.push(this.goodsList[i].name);
            setTimeout(() => {
              this.updateGoodData({
                ...this.goodsList[i],
                num: this.goodsList[i].num - 1
              });
            }, 350);
          });
          savaDataInfo({ ...this.dataForm, prescription: tempPerArr.join(",") })
            .then(res => {
              if (res && res.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  }
                });
              }
            })
            .catch(err => {
              console.log(err);
            });
        }
      });
    },
    //更新药品信息
    updateGoodData(data) {
      updateGoods({ ...data })
        .then(res => {
          console.log(res);
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  watch: {
    goodsList: {
      handler(val) {
        let tempArr = this.dataForm.prescription && this.dataForm.prescription.split(',') || []
        this.dataForm.prescription = tempArr.map(i => {
            return val.findIndex(item => item.name === i);
          });
          console.log(this.dataForm.prescription)
      },
      deep: true
    }
  }
};
</script>

<style scoped></style>